import { bg_image } from "./indoorConfig.js";
import {userCarList,getUserInfo} from "../api/home/index.js"
import {outdoorCheckDebt} from "../api/parkingRecords/index.js"
import {parkInsideContPay,parkInsidePay,staticRuleList,parkStaticRuleList} from "../api/scheme/index.js"
export default {
	data() {
		return {
			showOverlay:false,
			bg_image,
			schemeWidth: '',
			phone: '',
			parkList: [],
			parkStaticRuleList: [],
			form: {
				park: '',
				plateNumber: '',
				phoneNumber: '',
				ruleId: '',
			},
			payInfo: {},
			showError: false,
			disabled: false,
			isShowCurrentParkStaticRule: false,
			options:{},//页面参数
		}
	},
	watch: {
		'form.phoneNumber': {
			handler(newVal, oldVal) {
				if (/^1[3456789]\d{9}$/.test(newVal)) {
					this.showError = false
				} else {
					this.showError = true
				}
			}
		}
	},
	computed: {
		price() {
			let scheme = this.parkStaticRuleList.filter(item => {
				return this.form.ruleId === item.id
			})
			if (scheme.length) {
				return scheme[0].price
			} else {
				return ''
			}
		}
	},
	onLoad(options) {
		this.options = options
		this.fetchSomething(options)
	},
	onPullDownRefresh(){//页面下拉刷新
		this.fetchSomething(this.options)
		uni.stopPullDownRefresh();
	},
	methods: {
		async fetchSomething(options){
			this.getParkList()
			this.getPhoneNumber()
			this.getCarList()
			if (options.data) {
				this.disabled = true
				let data = JSON.parse(decodeURIComponent(options.data))
				this.form = {
					...this.form,
					...data
				}
				this.getParkStaticRule(this.form.parkId)
				this.isShowCurrentParkStaticRule = !!this.form.ruleId
			} else {
				this.disabled = false
			}
		},
		//获取默认车牌
		async getCarList() {
			let res = await userCarList()
			let data = res.data || []
			//续费月卡自带车牌不需要获取默认车牌
			if (data.length) {
				if (!this.form.plateNumber) {
					this.form.plateNumber = data.filter(item => {
						return item.defaultFlag === 1
					})[0].plateNumber
				}
			} else {
				this.form.plateNumber = ''
			}
		},
		//获取绑定手机号
		async getPhoneNumber() {
			let res = await getUserInfo()
			let data = res.data || {}
			uni.setStorageSync('userInfo',data)
			this.form.phoneNumber = data.phoneNumber || ''
		},
		checkPayForm() {
			if (this.form.ruleId && this.form.plateNumber && /^1[3456789]\d{9}$/.test(this.form.phoneNumber)) {
				return true
			} else {
				uni.showToast({
					title: '请填写完整信息',
					icon: "none"
				})
				return false
			}
		},
		//判断路侧是否存在欠费
		checkDebt() {
			return new Promise(async(resolve, reject) => {
				let res = await outdoorCheckDebt(this.form)
				let data = res.data
				if (data) {
					uni.showModal({
						title: '欠费提示',
						content: `您的车牌停车累计的欠费记录未缴清，点击下方《去缴费》进行补缴`,
						confirmText: '去缴费',
						confirmColor: '#2770FE',
						showCancel: false,
						success: (_res) => {
							if (_res.confirm) {
								uni.navigateTo({
									url: `/pages/indoor/unpaidOrder?parkId=${this.form.parkId}&plateNumber=${this.form.plateNumber}`
								})
							}
							resolve(true)
						}
					})
				} else {
					resolve(false)
				}
			})
		},
		async getPayInfo() {
			let flagDept = await this.checkDebt();
			if (flagDept) {
				return false;
			}
			if (this.checkPayForm()) {
				//续费
				if (this.disabled) {
					let res = await parkInsideContPay(this.form)
					let data = res.data || {}
					this.payInfo = data
					this.pay()
				} else {
					let res = await parkInsidePay(this.form)
					let data = res.data || {}
					this.payInfo = data
					this.pay()
				}

			}
		},
		async pay() {//调用支付宝支付
			let Provider =  await uni.getProvider({service: 'payment'})
			let payCanshu = {provider: Provider.provider[0] || 'wxpay',}
			payCanshu = {
				...payCanshu,
				orderInfo:this.payInfo.tradeNo,
			}
			let rePayment = await uni.requestPayment({...payCanshu});
			let flags = rePayment.errMsg == 'requestPayment:ok' && rePayment.resultCode != '6001'
			if(flags) {
				uni.redirectTo({
					url:'/pages/indoor/myScheme'
				})
			}else{
				this.$u.toast('支付失败');
			}
		},
		go(url) {
			if (this.disabled) {
				return false
			}
			uni.navigateTo({
				url,
				events: {
					changePlate: (data) => {
						this.form.plateNumber = data.plate
					}
				}
			})
		},
		open() {
			if (this.disabled) {
				return false
			}
			// this.$refs.popup.open('bottom')
			this.showOverlay = true
		},
		handleCancel() {
			this.showOverlay = false
		},
		handleConfirm() {
			this.showOverlay = false
		},
		async getParkList() {
			let res = await staticRuleList()
			let data = res.data || []
			this.parkList = data 
		},
		async getParkStaticRule(parkId) {
			let res = await parkStaticRuleList(parkId)
			let data = res.data || []
			this.parkStaticRuleList = data
			// this.$nextTick(() => {
			// 	this.calcSchemeWidth()
			// })
		},
		handleSelectPark(item) {
			this.form.parkId = item.id
			this.form.park = item.name
			this.showOverlay = false
			this.getParkStaticRule(this.form.parkId)
		},
		handleSelectRule(item) {
			this.form.ruleId = item.id
		},
		// calcSchemeWidth() {
		// 	let count = 0
		// 	const query = uni.createSelectorQuery().in(this);
		// 	const query2 = uni.createSelectorQuery().in(this);
		// 	query.selectAll('.scheme-item').boundingClientRect()
		// 	query.exec((res) => {
		// 		console.log('res',res)
		// 		count = res[0].length
		// 		if (res && res[0]) {
		// 			this.schemeWidth = res[0][0].width * count + 12 * count + 'px'
		// 		}
		// 	});
			
		// }
	},
	mounted() {}
}